package com.example.jetpacktest.databinding;

import android.graphics.Color;
import android.text.TextUtils;
import android.widget.ImageView;

import com.example.jetpacktest.R;
import com.squareup.picasso.Picasso;

import androidx.databinding.BindingAdapter;

class ImageViewBindingAdapter {
    @BindingAdapter("image")
    public static void setImage(ImageView imageView, String imageurl) {
        if (!TextUtils.isEmpty(imageurl)) {
            Picasso.get().load(imageurl).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
        } else {
            imageView.setBackgroundColor(Color.BLACK);
        }
    }

    @BindingAdapter("image")
    public static void setImage(ImageView imageView, int imageresl) {
        if (imageresl != -1) {
            Picasso.get().load(imageresl).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
        } else {
            imageView.setBackgroundColor(Color.BLACK);
        }
    }

    @BindingAdapter(value = {"image", "defaultImage"})
    public static void setImage(ImageView imageView, String imageurl,int imageres) {
        if (!TextUtils.isEmpty(imageurl)) {
            Picasso.get().load(imageurl).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
        } else {
            imageView.setBackgroundResource(imageres);
        }
    }
}
